package com.zxx.autohomenavi.page_com;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.zxx.autohomenavi.R;
import com.zxx.autohomenavi.base.BaseActivity;
import com.zxx.autohomenavi.fragment.TitleFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 适用于 同一个 Fragment
 */
public class CommonFragment2Activity extends BaseActivity {

    private FrameLayout frameLayout;
    private SegmentTabLayout tabLayout;
    private ArrayList<Fragment> fragmentList;
    private String[] titles = {"资讯", "话题", "我的"};

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_common_fragment2;
    }

    @Override
    protected void initView() {
        frameLayout = findViewById(R.id.frame_layout);
        tabLayout = findViewById(R.id.tab_layout);
    }

    @Override
    protected void onCreateEnd(Bundle savedInstanceState) {
        super.onCreateEnd(savedInstanceState);
        initData();
        initListener();

        tabLayout.setTabData(titles);

        //这个库里面的API可以代替FragmentManager及transaction的show与hide等
        //tabLayout.setTabData(titles, this, R.id.frame_layout, fragmentList);
    }

    private void initData() {
        fragmentList = new ArrayList<>();

        for (String key : titles) {
            Fragment fragment = new TitleFragment();
            Bundle bundle = new Bundle();
            bundle.putString("title", key);
            fragment.setArguments(bundle);
            fragmentList.add(fragment);

            //Fragment fragment1 = TitleFragment.getInstance(key);
            //fragmentList.add(fragment1);
        }

        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

        //添加最初的tab页
        fragmentTransaction.add(R.id.frame_layout, fragmentList.get(0))
                .commit();
    }

    private void initListener() {
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                for (Fragment fragment : fragmentList) {
                    transaction.hide(fragment);
                }

                if (fragmentList.get(position).isAdded()) {
                    Log.e("Tag-->", "s位置：" + position);
                    transaction.show(fragmentList.get(position));
                } else {
                    Log.e("Tag-->", "a位置：" + position);
                    transaction.add(R.id.frame_layout, fragmentList.get(position))
                            .show(fragmentList.get(position));
                }

                transaction.commit();
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }


    //------------------------------
    //-----  不同的 Fragment
    //------------------------------
    //private AFragment informationFragment;
    //private BFragment topicFragment;
    //private MineFragment mineFragment;

    @Override
    public void onAttachFragment(@NonNull Fragment fragment) {
        super.onAttachFragment(fragment);
        //if (fragment instanceof AFragment) {
        //    informationFragment = (AFragment) fragment;
        //}
    }

    private void initData2() {
        //informationFragment = new AFragment();
        //topicFragment = new BFragment();
        //mineFragment = new MineFragment();

        ArrayList<Fragment> fragmentList = new ArrayList<>();
        //fragmentList.add(informationFragment);
        //fragmentList.add(topicFragment);
        //fragmentList.add(mineFragment);

        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        //fragmentTransaction.add(R.id.frame_layout, informationFragment).commit();
    }

    private void initListener2() {
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                //transaction.hide(informationFragment);
                //transaction.hide(topicFragment);
                //transaction.hide(mineFragment);

                switch (position) {
                    case 0:
                        //if (informationFragment.isAdded()) {
                        //    transaction.show(informationFragment);
                        //} else {
                        //    transaction.add(R.id.frame_layout, informationFragment).show(informationFragment);
                        //}
                        break;
                    case 1:
                        //if (topicFragment.isAdded()) {
                        //    transaction.show(topicFragment);
                        //} else {
                        //    transaction.add(R.id.frame_layout, topicFragment);
                        //}
                        break;
                    case 2:
                        //if (mineFragment.isAdded()) {
                        //    transaction.show(mineFragment);
                        //} else {
                        //    transaction.add(R.id.frame_layout, mineFragment);
                        //}
                        break;
                }
                transaction.commit();
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

}